package com.zw.pdm.module.craft.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.craft.CraMakeDetail;
import com.zw.pdm.entity.craft.CraMakeSheet;
import com.zw.pdm.entity.craft.CraReleaseDetail;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.craft.model.ServiceTaskModel;
import com.zw.pdm.module.craft.model.TaskStatusModel;
import com.zw.pdm.module.craft.service.CraftReceiveService;
import com.zw.pdm.module.craft.service.CraftReleaseService;
import com.zw.pdm.module.report.service.SheetService;
import com.zw.pdm.module.system.service.DeptService;
import org.apache.poi.hssf.record.aggregates.DataValidityTable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @Description: 计划查询接口
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/craft/release")
@SuppressWarnings("all")
public class CraftReleaseController extends BaseController {


    @Autowired
    private CraftReleaseService craftReleaseService;
    @Autowired
    private SheetService sheetService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private DictCommonService dictCommonService;

    @GetMapping
    @RequiresPermissions({"tec:proTaskrel:view"})
    public String page(Model model) {
        model.addAttribute("specialId", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("sectionId", craftReleaseService.findSectionByDeptId(1));
        model.addAttribute("craSection", craftReleaseService.findCraftSectionByDeptId(4));
//        return "craft/release/page";
        return "craft/release/releasePage";
    }

    /**
     * 接收列表
     */
    @ResponseBody
    @PostMapping("/list")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult list(ServiceTaskModel serviceTaskModel) {
        startPage();
        List<ServiceTaskModel> list = craftReleaseService.findWithCraftQuery(serviceTaskModel);
        return jsonResult(list);
    }
    /**
     * 分配任务
     */
    @ResponseBody
    @PostMapping("/info")
    @RequiresPermissions({"tec:proRelease:edit"})
    public JsonResult info(@RequestParam("makeId") Integer makeId, @RequestParam("sectionId") Integer[] sectionId,  Model model) {
        return jsonResult(craftReleaseService.releaseTask(makeId, sectionId));
    }
    /**
     * 重新分配任务按钮加载项
     */
    @ResponseBody
    @PostMapping("/redistribution")
    @RequiresPermissions({"tec:proChange:edit"})
    public JsonResult redistribution(Integer fkId) {
        List<CraReleaseDetail> craReleaseDetailList= craftReleaseService.selTaskList(fkId);
        for (int i = 0; i <craReleaseDetailList.size() ; i++) {
            if (craReleaseDetailList.get(i).getIsSuccess()==1){
                return  JsonResult.fail();
            }
        }
        return jsonResult(craftReleaseService.selTaskList(fkId));
    }

}
